using Framework;
using UnityEngine;
using UnityEngine.AI;

public class Npc : ActorBase
{
    public bool Moving { get; protected set; }
    public Vector3 MoveDestination { get; protected set; }
    public Vector3 Position
    {
        get
        {
            return GameObject != null ? GameObject.transform.position : Vector3.zero;
        }
    }
    public Vector3 MoveDirection;
    protected NavMeshAgent _agent;
    protected Animator _animator;
    private byte _moveStatus;
    public ActorBase InteractiveTarget;
    private bool _started;
    private bool _agentEnabled;
    public GameObject GetDisplay()
    {
        return GameObject;
    }
    public NavMeshAgent GetNavmeshAgent()
    {
        return _agent;
    }
    public Animator GetAnimator()
    {
        return _animator;
    }
    protected override void OnStart()
    {
        base.OnStart();
        if (!_agentEnabled)
        {
            _agent = this.GameObject.GetComponent<NavMeshAgent>();
            _animator = this.GameObject.GetComponent<Animator>();
            _agentEnabled = _agent != null;
        }
        _started = true;
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
        if (_agent != null)
        {
            _agent.enabled = false;
        }
        Moving = false;
        _moveStatus = 0;
    }
    protected override void OnUpdate(float deltaTime, float unscaledDeltaTime)
    {
        base.OnUpdate(deltaTime, unscaledDeltaTime);
        if (_started)
        {
            _started = false;
            if (_agentEnabled)
            {
                _agent.autoBraking = true;
                _agent.enabled = true;
            }
        }
        if (_agentEnabled)
        {
            checkMoving();
        }
    }
    public void MoveTo(Vector3 worldPosition)
    {
        MoveDestination = worldPosition;
        if (!Moving)
        {
            _moveStatus = 1;
        }
        Moving = true;
    }
    public void MoveStop()
    {
        if (!Moving) return;
        Moving = false;
        _moveStatus = 0;
        if (_agent != null && _agent.enabled)
        {
            _agent.isStopped = true;
            _agent.ResetPath();
        }
        // PlayAnimation("Idle", 0.2f);
        onMoveEnd();
    }
    public void WarpTo(Vector3 worldPosition)
    {
        MoveDestination = worldPosition;
        if (_agent != null)
        {
            _agent.enabled = false;
            _agent.Warp(worldPosition);
            _agent.enabled = true;
        }
        Moving = true;
    }
    public void PlayAnimation(string name, float fadeTime = 0.2f)
    {
        if (_animator != null)
        {
            _animator.CrossFadeInFixedTime(name, fadeTime);
        }
    }
    private void checkMoving()
    {
        if (!Moving) return;
        if (!_agent.isOnNavMesh) return;
        if (_moveStatus == 1)
        {
            //请求移动
            _moveStatus = 2;
            _agent.SetDestination(MoveDestination);
        }
        else if (_moveStatus == 2)
        {
            //检测是否移动了
            if (_agent.pathPending) return;
            if (_agent.pathStatus == NavMeshPathStatus.PathInvalid || _agent.pathStatus == NavMeshPathStatus.PathPartial)
            {
                MoveStop();
                return;
            }
            _moveStatus = 3;
            // PlayAnimation("Run", 0.2f);
            onMoveStart();
        }
        else if (_moveStatus == 3)
        {
            _moveStatus = 4;
            _agent.SetDestination(MoveDestination);
            if (!_agent.hasPath)
            {
                MoveStop();
            }
        }
    }
    protected virtual void onMoveStart()
    {

    }
    protected virtual void onMoveEnd()
    {

    }

    public override Vector3 GetWorldPosition()
    {
        return GameObject.transform.position;
    }
    public void SetAnimationSpeed(float value)
    {
        if (_animator != null)
        {
            _animator.speed = value;
        }
    }

}